<?php
class perusahaan extends ci_controller
{

	function __construct(){
		
		parent::__construct();
		$this->load->library('tank_auth');
		$this->load->library('bamer_lib');
		$this->load->model('modul/perusahaan_model');
		$this->load->model('user');
	//	$this->load->model('page');
	//	$this->load->model('notification');

		if (!$this->tank_auth->is_logged_in()) redirect('/auth/login/');
		$this->load->helper('bamer_helper');
		$this->lang->load('tank_auth');
		
	}

	function index(){

		//check_permission
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end
		$data['rows'] = $this->perusahaan_model->list_perusahaan();
		$data['menu'] = 'kelola_perusahaan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/index';
		$this->load->view('layout/inner', $data);
	}

	function show(){
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Data Perusahaan');

		$id = $this->uri->segment(4);

		$where = array('id_perusahaan' => $id);
		$where2 = array('id_global' => $id, 'id_user_group' => 6);
		$data['perusahaan'] = $this->user->edit('perusahaan', $where);
		$data['kontak'] = $this->user->edit('kontak', $where2);
		
		$data['menu'] = 'kelola_perusahaan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/show';
		$this->load->view('layout/inner', $data);
	}
	

	function add(){
		
		//check_permission
		$row = $this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end
		
		$data['menu'] = 'kelola_perusahaan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/add';
		$this->load->view('layout/inner', $data);
	}

	function edit(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end

		$id = $this->uri->segment(4);

		$where = array('id_perusahaan' => $id);
		$where2 = array('id_global' => $id, 'id_user_group' => 6);
		$data['perusahaan'] = $this->user->edit('perusahaan', $where);
		$data['kontak'] = $this->user->edit('kontak', $where2);
		
		
		$data['menu'] = 'kelola_perusahaan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/edit';
		$this->load->view('layout/inner', $data);
	
	}

	function create(){

		//check_permission
		$this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end

		//validation
		$this->form_validation->set_rules('nama_perusahaan', 'Nama', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('alamat_perusahaan', 'Alamat', 'trim|required|min_length[5]|xss_clean');


		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = 'kelola_perusahaan';
			$data['head'] = 'layout/head';
	        $data['navbar'] = 'layout/navbar';
			$data['content'] = 'modul/perusahaan/add';
			$this->load->view('layout/inner', $data);
		}
		else{

			$data_perusahaan = array(
				'nama_perusahaan' => $this->input->post('nama_perusahaan'),
				'kategori_perusahaan' => $this->input->post('kategori_perusahaan'),
				'alamat_perusahaan' => $this->input->post('alamat_perusahaan'),
				'keterangan_perusahaan' => $this->input->post('keterangan_perusahaan'),
				'longitude_perusahaan' => $this->input->post('longitude_perusahaan'),
				'latitude_perusahaan' => $this->input->post('latitude_perusahaan'),
				'status_marketing_perusahaan' => 'belum',
				'id_user_group' => 6,
				);

			$id_perusahaan = $this->user->insert_get('perusahaan', $data_perusahaan);

			$rows = $this->perusahaan_model->get_perusahaan();
			foreach ($rows as $row) {
				$data_penugasan = array(
				'id_perusahaan' => $row->id_perusahaan,
				);
			}

		$id_penugasan = $this->user->insert_get('penugasan_marketing', $data_penugasan);
			
			$banyak_kontak = $this->input->post('kontak');
			$tipe_kontak = $this->input->post('tipe_kontak');


			$count_banyak_kontak = count($banyak_kontak);

			for($i='1'; $i <= $count_banyak_kontak; $i++){
				if($banyak_kontak[$i]==""){
					$kosong[$i]='1';
				}
			}
			
			$count_kontak_kosong = count($kosong);

			$count = $count_banyak_kontak-$count_kontak_kosong;
			for($i='1'; $i <= $count; $i++){
				$data_kontak[$i] = array(
					'id_user_group' => 6,
					'tipe_kontak' => $tipe_kontak[$i],
					'kontak' => $banyak_kontak[$i],
					'id_global' => $id_perusahaan,
					);

				$this->user->insert('kontak', $data_kontak[$i]);
			}
		
			redirect('modul/perusahaan/index'); 
		}

		
	}

	function update(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end

		//validation
		$this->form_validation->set_rules('nama_perusahaan', 'Nama', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('alamat_perusahaan', 'Alamat', 'trim|required|min_length[5]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->input->post('id_perusahaan');

			$where = array('id_perusahaan' => $id);
			$where2 = array('id_global' => $id, 'id_user_group' => 6);
			$data['perusahaan'] = $this->user->edit('perusahaan', $where);
			$data['kontak'] = $this->user->edit('kontak', $where2);
			
			
			$data['menu'] = 'kelola_perusahaan';
			$data['head'] = 'layout/head';
	        $data['navbar'] = 'layout/navbar';
			$data['content'] = 'modul/perusahaan/edit';
			$this->load->view('layout/inner', $data);
		}

		else{
			$data_perusahaan = array(
				'nama_perusahaan' => $this->input->post('nama_perusahaan'),
				'kategori_perusahaan' => $this->input->post('kategori_perusahaan'),
				'alamat_perusahaan' => $this->input->post('alamat_perusahaan'),
				'keterangan_perusahaan' => $this->input->post('keterangan_perusahaan'),
				'longitude_perusahaan' => $this->input->post('longitude_perusahaan'),
				'latitude_perusahaan' => $this->input->post('latitude_perusahaan'),
				'status_marketing_perusahaan' => 'belum',
				);
		

			$id_perusahaan = $this->input->post('id_perusahaan');
			$where_perusahaan = array('id_perusahaan' => $id_perusahaan);
			$this->user->update('perusahaan',$where_perusahaan, $data_perusahaan);

			
			if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
				foreach($this->input->post('delete_ids') as $id_kon) {
					$where = array('id_kontak' => $id_kon);
					$this->user->delete('kontak', $where);
				}
			}

			$where = array('id_global' => $id_perusahaan, 'id_user_group' => 6);
			$rows = $this->user->edit('kontak', $where);
			foreach($rows as $r){
				$where_kontak[$r->id_kontak] = array('id_kontak' => $r->id_kontak,'id_global' => $id_perusahaan, 'id_user_group' => 6);

				$data_kontak[$r->id_kontak] = array(
					'tipe_kontak' => $this->input->post('tipe_kontak')[$r->id_kontak],
					'kontak' => $this->input->post('kontak')[$r->id_kontak],
					);

				$this->user->update('kontak', $where_kontak[$r->id_kontak], $data_kontak[$r->id_kontak]);
			}

			if(!empty($_POST['kontak'])){

				$banyak_kontak = $this->input->post('kontak');
				$tipe_kontak = $this->input->post('tipe_kontak');

				$count_banyak_kontak = count($banyak_kontak);

				for($i='2'; $i <= $count_banyak_kontak; $i++){
					if($banyak_kontak[$i]==""){
						$kosong[$i]='1';
					}
				}
				
				$count_kontak_kosong = count($kosong);

				$count = $count_banyak_kontak-$count_kontak_kosong;
				for($i='2'; $i <= $count; $i++){
					$data_kontak[$i] = array(
						'id_user_group' => 6,
						'tipe_kontak' => $tipe_kontak[$i],
						'kontak' => $banyak_kontak[$i],
						'id_global' => $id_perusahaan,
						);

					$this->user->insert('kontak', $data_kontak[$i]);
				}

			}

			redirect('modul/perusahaan/index');
		}

	}

	function delete(){
		//check_permission
		$this->bamer_lib->check_permission_delete($this->tank_auth->get_user_id(), 'Data Perusahaan');
		//end

		$id_perusahaan = $this->uri->segment(4);

		$where = array('id_perusahaan' => $id_perusahaan);
		$this->user->delete('perusahaan', $where);
		$where = array('id_global' => $id_perusahaan, 'id_user_group' => 6);
		$this->user->delete('kontak', $where);
		$where = array('id_perusahaan' => $id_perusahaan);
		$this->user->delete('penugasan_marketing', $where);

		$get_id = $this->perusahaan_model->get_id_penugasan_marketing($id_perusahaan);
		
		$where = array('id_penugasan_marketing' => $get_id);
		$this->user->delete('penugasan_followup', $where);
		
		redirect('modul/perusahaan/index');
	}

	function search(){

		$keyword = $this->input->post('cari');

		if($keyword == 'Cari Perusahaan...') $keyword = "";
		else $keyword = $keyword;

		$data['rows'] = $this->perusahaan_model->get_perusahaan_search($keyword);
		$data['menu'] = 'kelola_perusahaan';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/index';
		$this->load->view('layout/inner', $data);

	}

	function ambildata(){

		$json = '{"wilayah": {';
		$json .= '"petak":[ ';
		foreach($this->perusahaan_model->list_perusahaan() as $val){
		    $json .= '{';
		    $json .= '"id":"'.$val->id_perusahaan.'",
		             "judul":"'.htmlspecialchars($val->nama_perusahaan).'",
		             "deskripsi":"'.$val->nama_perusahaan.'",
		             "x":"'.$val->latitude_perusahaan.'",
		             "y":"'.$val->longitude_perusahaan.'",
		             "lokasi":"'.$val->nama_perusahaan.'",
		             "jenis":"'.$val->status_marketing_perusahaan.'"
		             },';

		}

		$json = substr($json,0,strlen($json)-1);
		$json .= ']';

		$json .= '}}';
		echo utf8_encode($json);

	}

	function list_sudah_marketing(){
		$data['rows'] = $this->perusahaan_model->get_sudah_marketing();

		$data['head'] = 'layout/head';
		$data['menu'] = 'detil';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/hasil';
		$this->load->view('layout/inner', $data);

	}

	function detail_sudah_marketing(){
		$id_perusahaan = $this->uri->segment(4);
		$data['kontak'] = $this->perusahaan_model->list_kontak();
		$data['rows'] = $this->perusahaan_model->detail_hasil($id_perusahaan);
		$data['head'] = 'layout/head';
		$data['menu'] = 'detil';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/perusahaan/detail';
		$this->load->view('layout/inner', $data);

	}
}
?>